package com.example.cloudmusic.home.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.cloudmusic.databinding.ItemArtistCardBinding
import com.example.cloudmusic.home.model.Artist
import com.example.cm_utils.FormatUtils

/**
 * 艺人适配器
 */
class ArtistAdapter : BaseListAdapter<Artist, ArtistAdapter.ViewHolder>(ArtistDiffCallback()) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            ItemArtistCardBinding.inflate(
                LayoutInflater.from(parent.context), parent, false
            )
        )
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(getItem(position))
    }

    inner class ViewHolder(private val binding: ItemArtistCardBinding) : 
        RecyclerView.ViewHolder(binding.root) {
        
        init {
            binding.root.setOnClickListener { onItemClick(adapterPosition) }
        }
        
        fun bind(item: Artist) {
            binding.apply {
                cmArtistName.text = item.name
                cmArtistFollowers.text = "粉丝: ${FormatUtils.formatFollowerCount(item.followers)}"
                
                Glide.with(root)
                    .load(item.imageUrl)
                    .circleCrop()
                    .into(cmArtistAvatar)
            }
        }
    }

    private class ArtistDiffCallback : DiffUtil.ItemCallback<Artist>() {
        override fun areItemsTheSame(oldItem: Artist, newItem: Artist): Boolean =
            oldItem.id == newItem.id

        override fun areContentsTheSame(oldItem: Artist, newItem: Artist): Boolean =
            oldItem.name == newItem.name && 
            oldItem.imageUrl == newItem.imageUrl && 
            oldItem.followers == newItem.followers
    }
} 